//https://leetcode.cn/problems/trim-a-binary-search-tree/
package codeRandomThoughts.Test669修剪二叉搜索树;

import codeRandomThoughts.utils.TreeNode;

public class Solution2 {
    //返回一个修剪好的二叉搜索树的根节点
    public TreeNode trimBST(TreeNode root, int low, int high) {
        //终止条件
        if (root == null) return root;

        //递归逻辑
        if (root.val < low) {
            //删除root以及其左子树
            return trimBST(root.right, low, high);
        }
        if (root.val > high) {
            //删除root以及其右子树
            return trimBST(root.left, low, high);
        }

        //root的值满足条件 修剪子树即可
        root.left = trimBST(root.left, low, high);
        root.right = trimBST(root.right, low, high);
        return root;
    }
}
